#include <stdio.h>
#include <stdlib.h>

#define MAX 30000

int f[MAX + 1];
int r[MAX + 1];
int above[MAX + 1];

int init()
{
    int i;
    for (i = 1; i <= MAX; i++) {
        above[i] = 0;
        f[i] = i;
        r[i] = 1;
    }

    return 0;
}

int realfather;
int find(int n)
{
    int t;

    if (n == f[n]) {
        realfather = n;
        return n;
    } else {
        t = find(f[n]);
        if (f[n] != realfather) {
            above[n] += (above[f[n]]);
        }
        f[n] = t;
    }

    return f[n];
}

int merge(int x, int y)
{
    int a = find(x);
    int b = find(y);

    f[b] = a;
    above[b] += r[a];
    r[a] += r[b];

    return 0;
}

int main (int argc, char const* argv[])
{

    int p, i;
    init();

    char order;
    int a, b;
    scanf("%d", &p);
    for (i = 1; i <= p; i++) {
        getchar();
        scanf("%c", &order);
        if (order == 'M') {
            scanf("%d %d", &a, &b);
            merge(a, b);
        } else if (order == 'C') {
            scanf("%d", &a);
            printf("%d\n", r[find(a)] - above[a] - 1);
        }
    }

    return 0;
}
